<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>构建代码</title>
	<link rel="stylesheet" href="/component/pear/css/pear.css" />
	<script src="/component/layui/layui.js"></script>
	<script src="/component/pear/pear.js"></script>
	<style>

	</style>
</head>

<body>
	<form class="layui-form" action="" lay-filter="form">
		<div class="mainBox">
			<div class="layui-tab layui-tab-brief" id="tab" lay-filter="tab">

			</div>
		</div>
		<div class="bottom">
			<div class="button-container">
				<button type="submit" class="pear-btn pear-btn-sm pear-btn-primary" lay-submit="" lay-filter="save">
					<i class="icon pear-icon pear-icon-select-bold"></i>
					生成
				</button>
				<button type="button" lay-click="layerclose" class="pear-btn pear-btn-sm">
					<i class="icon pear-icon pear-icon-close-bold"></i>
					关闭
				</button>
			</div>
		</div>
	</form>
</body>

</html>


<script type="text/html" id="tabTpl">
	<ul class="layui-tab-title">
		{{#  layui.each(d.list, function(index, data){ }}
		{{#  if(index === 0){ }}
		<li class="layui-this">{{data.title}}</li>
		{{#  }else{ }} 
		<li>{{data.title}}</li>
		{{#  } }} 
		{{#  }); }}
	  </ul>
	  <div class="layui-tab-content">
		{{#  layui.each(d.list, function(index, data){ }}
		{{#  if(index === 0){ }}
		<div class="layui-tab-item layui-show">
			<xmp style="padding:0 25px">{{data.content}}</xmp>
		</div>
		{{#  }else{ }} 
		<div class="layui-tab-item">
			<xmp style="padding:0 25px">{{data.content}}</xmp>
		</div>
		{{#  } }} 
		{{#  }); }}
	  </div>
</script>

<script>
	layui.use(['form', 'jquery', 'context', 'permission', 'popup', 'tree', 'util', 'common', 'button', 'laytpl'], function () {
		var form = layui.form;
		var $ = layui.jquery;
		var context = layui.context;
		var popup = layui.popup;
		var tree = layui.tree;
		var util = layui.util;
		var common = layui.common;
		var button = layui.button;
		var laytpl = layui.laytpl;

		var basePath = context.get("base-path");
		var buildWindowName = common.getQueryString("buildWindowName");

		var dataJson = top.window.frames[buildWindowName].getDataJson();//获取上一个页面的json
		console.log(dataJson)

		$.ajax({
			url: basePath + '/api/system/code/codepreview',
			data: JSON.stringify(dataJson),
			success: function (result) {
				if (result.succeeded) {
					let getTpl = tabTpl.innerHTML;
					let view = document.getElementById('tab');
					laytpl(getTpl).render({ list: result.data }, function (html) {
						view.innerHTML = html;
						layui.code();
					});
				} else {
					popup.failure(JSON.stringify(result.errors));
				}
			}
		})

		form.on("submit(save)", function (data) {
			var btn = button.load({ elem: "button[lay-filter=save]" });
			$.ajax({
				url: basePath + '/api/system/code/buildcode',
				data: JSON.stringify(dataJson),
				success: function (result) {
					btn.stop(function () {
						if (result.succeeded) {
							top.layer.alert(result.data, function (index) {
								//关闭弹窗
								top.layer.close(index);
								top.layer.close(top.layer.getFrameIndex(window.name));//关闭当前页弹窗
								top.layer.close(top.layer.getFrameIndex(buildWindowName));//关闭构建页弹窗
							})
						} else {
							popup.failure(JSON.stringify(result.errors));
						}
					})
				}
			})

			return false;
		});

		util.event('lay-click', {
			layerclose: function (othis) {//关闭当前弹窗
				top.layer.close(top.layer.getFrameIndex(window.name));//关闭当前页
			},
		});
	})
</script>